import {randomString} from "../../../../utils/util";
import LocaleUtil from "../../../../utils/LocaleUtil";
import {postAction} from '@/api/manage'

export const LocaleMixin = {
  props: {},
  data() {
    const codeValid = ({cellValue, row}) => {
      if (cellValue) {
        const $table = this.$refs.xTable
        const {tableData} = $table.getTableData()
        const uniqueRow = tableData.find(item => item.code && item.code.toLowerCase() === cellValue.toLowerCase() && item.id !== row.id)
        if (uniqueRow) {
          return new Error(this.$t('m.common.tip.code_unique'))
        }
      }
    }
    return {
      editStatus: false,
      tableData: [],
      loading: 0,
      validRules: {
        code: [
          {required: true, message: this.$t('m.common.tip.code_required')},
          {validator: codeValid},
        ]
      },
    }
  },
  created() {
    this.$nextTick(() => {
      // 将表格和工具栏进行关联
      const $table = this.$refs.xTable
      $table.connect(this.$refs.xToolbar)
    })
  },
  methods: {
    async randomCodeEvent() {
      const $table = this.$refs.xTable
      const {tableData} = $table.getTableData()
      tableData.forEach(item => {
        if (!item.code) {
          item.code = randomString(8)
        }
      })
      await $table.clearEdit()
    },
    async insertEvent() {
      const $table = this.$refs.xTable
      const newRecord = {
        id: randomString(32)
      }
      if (this.groupId) {
        newRecord['groupId'] = this.groupId
      }
      const {row: newRow} = await $table.insert(newRecord)
      await $table.setActiveRow(newRow)
    },
    async removeSelectEvent() {
      const $table = this.$refs.xTable
      await $table.removeCheckboxRow()
    },
    async removeRowEvent(row) {
      const $table = this.$refs.xTable
      await $table.remove(row)
    },
    async saveEvent() {
      this.loading++
      const $table = this.$refs.xTable
      const {insertRecords, removeRecords, updateRecords} = $table.getRecordset()
      if (insertRecords.length <= 0 && removeRecords.length <= 0 && updateRecords.length <= 0) {
        this.messageUtil.warning(this.$t('m.eform.localeProvider.label.unchanged'))
        this.loading--
        return
      }
      const errMap = await $table.validate().catch(errMap => errMap)
      if (errMap) {
        this.loading--
        return
      }
      const body = {insertRecords, removeRecords, updateRecords}
      const res = await postAction(this.url.saveLocales, body)
      if (res.success) {
        this.messageUtil.success(res.message)
        LocaleUtil.refreshLocale();
        await this.loadList()
        this.editStatus = false
      } else {
        this.messageUtil.warning(res.message)
      }
      this.loading--
    },
    async copyFromTable(auto) {
      this.loading++
      const res = await postAction(this.url.copyFromTable + `?tableName=${this.tableName}`)
      if (res.success) {
        LocaleUtil.refreshLocale();
        if (!auto) {
          this.messageUtil.success(res.message)
          await this.loadList()
        }
      } else {
        this.messageUtil.warning(res.message)
      }
      this.loading--
    }
  },
}
